<template>
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 收货地址 -->
    <adress></adress>
    <!-- 购物车头部标题栏 -->
    <view class="cart-header">
      <view class="cart-title">
        <!-- 购物车标题图标 -->
        <uni-icons type="shop" size="16"></uni-icons>
        <!-- 购物车标题文字 -->
        <view class="cart-title-content">购物车</view>
      </view>
    </view>
    <!-- 购物车商品列表 -->
    <uni-swipe-action>
      <block v-for="(item, index) in cart" :key="index">
        <uni-swipe-action-item :options="options" @click="removeGoods(item)">
          <goods_items :goods="item" :showRadio="true" :showNum="true" @checkedChange="radiosHandler" @numChange="numHandler"></goods_items>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 结算区域 -->
    <settle></settle>    
  </view>
  
  
  <!-- 购物车没有商品时展示的页面 -->
  <view class="notGoods" v-else>
    <!-- 图片 -->
    <view class="notGoods-images">
      <image src="/static/cart_empty@2x.png" mode="widthFix"></image>
    </view>
    <!-- 文字 -->
    <view class="not">
      空空如也~
    </view>
  </view>
</template>

<script>
// 导入badgeMixins文件
import badgeMixins from '../../mixins/badgeMixins.js';
// 从 vuex 中按需导入 mapState 函数
import { mapState, mapMutations } from 'vuex';
export default {
  // 挂载导入的badgeMixins
  mixins: [badgeMixins],
  data() {
    return {
      options: [
        {
          text: '删除',
          style: {
            backgroundColor: '#ff3f34'
          }
        }
      ]
    };
  },
  computed: {
    ...mapState('cart', ['cart'])
  },
  methods: {
    // 将 cart 仓库中的 changeChecked, changeNum 方法映射为当前组件的方法
    ...mapMutations('cart', ['changeChecked', 'changeNum', 'deleteGoods']),
    // 单选框自定义事件
    radiosHandler(goods) {
      this.changeChecked(goods);
    },
    // 计数器值发生变化的自定义事件
    numHandler(goods) {
      this.changeNum(goods);
    },
    // 删除按钮点击事件事件处理函数
    removeGoods(goods) {
      // console.log(goods)
      this.deleteGoods(goods)
    }
  },
  onLoad() {
    console.log(this.cart);
  }
};
</script>

<style lang="scss">
.cart-container {
  padding-bottom: 50px;
  .cart-header {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #efefef;
    height: 40px;
    .cart-title {
      display: flex;
      align-items: center;
      padding-left: 5px;
      .cart-title-content {
        font-size: 14px;
        margin-left: 5px;
      }
    }
  }
}

.notGoods {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;
  .notGoods-images {
    width: 90px;
    height: 90px;
    image {
      width: 100%;
    }
  }
  .not {
    margin-top: 10px;
    font-size: 12px;
    color: gray;
  }
}
</style>
